package com.qf.service.impl;

/*
 *  @author Administrator
 *  @version V1.0
 *  @Project banwoqiche
 *  @Package com.qf.service.impl
 *  @Description
 *  @Date2022/3/30 0030 21:55
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.article.Article;
import com.qf.bean.article.ArticleCategory;
import com.qf.bean.article.ArticleTag;
import com.qf.bean.article.dto.Audit;
import com.qf.bean.article.dto.Dratf;
import com.qf.exception.DeleteException;
import com.qf.exception.UpdateException;
import com.qf.mapper.ArticleMapper;
import com.qf.service.ArticleService;
import com.qf.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
@Service
public class ArticleServiceImpl implements ArticleService {
    private ArticleMapper articleMapper;

    @Autowired
    public void setArticleMapper(ArticleMapper articleMapper) {
        this.articleMapper = articleMapper;
    }

    @Override
    public PageInfo<Article> findAll(Integer page,Integer limit) {
        PageHelper.startPage(page, limit);
        List<Article> articleList=articleMapper.findAll();
        PageInfo<Article> pageInfo = new PageInfo<>(articleList);
        pageInfo.setPageNum(page);
        pageInfo.setTotal(articleList.size());
        return pageInfo;
    }

    @Override
    public PageInfo<Article> findDrafts(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Article> articleList=articleMapper.findDrafts();
        PageInfo<Article> pageInfo = new PageInfo<>(articleList);
        pageInfo.setPageNum(page);
        pageInfo.setTotal(articleList.size());
        return pageInfo;
    }

    @Override
    public int submitDrafts(Integer articleId) {
        return articleMapper.submitDrafts(articleId);
    }

    @Override
    public int deleteDraft(Integer articleId) {
        Assert.isTrue(articleId>0,()->{
            throw new DeleteException( "数据选择不正确",ResultCode.DELETEFAIL);
        });
        return articleMapper.deleteDraft(articleId);
    }

    @Override
    public Dratf selectDratf(Integer articleId) {
        Assert.isTrue(articleId>0,()->{
            throw new DeleteException( "数据选择不正确",ResultCode.DELETEFAIL);
        });
        return articleMapper.selectDratf(articleId);
    }

    @Override
    public int updateDratf(Integer articleId, Dratf dratf) {
        int i = articleMapper.updateDratf(articleId, dratf);
        return i;
    }

    @Override
    public PageInfo<Article> findAudit(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Article> articleList=articleMapper.findAudit();
        PageInfo<Article> pageInfo = new PageInfo<>(articleList);
        pageInfo.setPageNum(page);
        pageInfo.setTotal(articleList.size());
        return pageInfo;
    }

    @Override
    public int passAudit(Integer articleId) {
        Assert.isTrue(articleId>0,()->{
            throw new DeleteException( "数据选择不正确",ResultCode.DELETEFAIL);
        });
        int i = articleMapper.passAudit(articleId);
        Assert.isTrue(i>0,()->{
            throw new UpdateException("操作失败", ResultCode.UPDATEFAIL);
        });
        return i;
    }

    @Override
    public int noPassAudit(Integer articleId) {
        Assert.isTrue(articleId>0,()->{
            throw new DeleteException( "数据选择不正确",ResultCode.DELETEFAIL);
        });
        int i = articleMapper.noPassAudit(articleId);
        Assert.isTrue(i>0,()->{
            throw new UpdateException("操作失败", ResultCode.UPDATEFAIL);
        });
        return i;
    }

    @Override
    public Audit selectAudit(Integer articleId) {
        Assert.isTrue(articleId>0,()->{
            throw new DeleteException( "数据选择不正确",ResultCode.DELETEFAIL);
        });
        return articleMapper.selectAudit(articleId);
    }

    @Override
    public PageInfo<Article> findIssue(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Article> articleList=articleMapper.findIssue();
        PageInfo<Article> pageInfo = new PageInfo<>(articleList);
        pageInfo.setPageNum(page);
        pageInfo.setTotal(articleList.size());
        return pageInfo;
    }

    @Override
    public List<ArticleTag> findAllTag() {
        return articleMapper.findAllTag();
    }

    @Override
    public List<ArticleCategory> findAllCategory() {
        return articleMapper.findAllCategory();
    }

    @Override
    public List<Article> findAction() {
        return articleMapper.findAction();
    }
}
